<?php
class Server {
  public static function get_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $data = $orm->get('pb_ecom_order', NULL, NULL, NULL, NULL, $opts)->d;
    
    if (count($data) == 0) {
      return;
    }
    
    $opts = new stdClass();
    $opts->cd = 't.order_id = :v1';
    $opts->cdv = array(':v1' => $data[0]->id);
    $data[0]->__purchases = $orm->get('pb_ecom_purchase', NULL, NULL, NULL, NULL, $opts)->d;
    
    echo json_encode($data);
  }
  
  public static function submit_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    $orm->update('pb_ecom_order', $req);
    
    if (isset($req->__pg)) {
      $req->__objs = $orm->get('pb_ecom_order', 't.order_number', 'DESC', $req->__pg, SIZE, NULL);
    }
    
    echo json_encode($req);
  }
}
?>